<?php
session_cache_expire(999999999);
if (!isset($_SESSION)) {
    session_start();
}

if (isset($_SESSION['lang']))
    $lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
    include "../value/value_en.php";
} else if ($lang == "VN") {
    include "../value/value_vn.php";
} else {
    include "../value/value_kr.php";
}
$list = $_SESSION['sort'];
$str = $_REQUEST['str'];
if (isset($_REQUEST['co']) && $_REQUEST['co'] == 1) {

    if ($list[$str] == "DESC")
        $list[$str] = "ASC";
    else
        $list[$str] = "DESC";
}
include '../operations/connection.php';

if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'search') {

    $list["l.time"] = "DESC";
    $list["l.type_money"] = "DESC";
    $list["l.type_trade"] = "DESC";
    $list["l.value"] = "DESC";
    $list["e.FullName"] = "DESC";

    $_SESSION['sort'] = $list;

    $fromdate = $_REQUEST['fromdate'];
    $todate = $_REQUEST['todate'];
    //$sTrade=$_REQUEST['sTrade'];
    //if($sTrade!=-1)
    //	$_SESSION['searchMoney']=" AND l.time >='$fromdate' AND l.time<='$todate' AND l.type_trade='$sTrade' ";
    //else
    $_SESSION['searchMoney'] = " AND l.time >='$fromdate' AND l.time<='$todate' ";

    $logmoney = mysql_query("SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName,l.note FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 " . $_SESSION['searchMoney'] . " ORDER BY " . $str . " " . $list[$str] . ',id DESC');

    //echo "SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 ".$_SESSION['searchMoney']." ORDER BY ".$str." ".$list[$str];

    if (isset($_REQUEST['page'])) {
        $page = $_REQUEST['page'];
    } else {
        $page = 1;
    }
    $rows_per_page = 10;
    $page_start = ( $page - 1 ) * $rows_per_page;
    $page_end = $page * $rows_per_page;
    $number_of_page = ceil(mysql_num_rows($logmoney) / $rows_per_page);
} else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'searchAll') {

    $list["l.time"] = "DESC";
    $list["l.type_money"] = "DESC";
    $list["l.type_trade"] = "DESC";
    $list["l.value"] = "DESC";
    $list["e.FullName"] = "DESC";

    $_SESSION['sort'] = $list;

    $fromdate = $_REQUEST['fromdate'];
    $todate = $_REQUEST['todate'];
    $_SESSION['searchMoney'] = "";

    $logmoney = mysql_query("SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName,l.note FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 " . $_SESSION['searchMoney'] . " ORDER BY " . $str . " " . $list[$str] . ',id DESC');

    //echo "SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 ".$_SESSION['searchMoney']." ORDER BY ".$str." ".$list[$str];
    if (isset($_REQUEST['page'])) {
        $page = $_REQUEST['page'];
    } else {
        $page = 1;
    }
    $rows_per_page = 10;
    $page_start = ( $page - 1 ) * $rows_per_page;
    $page_end = $page * $rows_per_page;
    $number_of_page = ceil(mysql_num_rows($logmoney) / $rows_per_page);
} else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'searchToday') {

    $list["l.time"] = "DESC";
    $list["l.type_money"] = "DESC";
    $list["l.type_trade"] = "DESC";
    $list["l.value"] = "DESC";
    $list["e.FullName"] = "DESC";

    $_SESSION['sort'] = $list;

    $fromdate = date("Y-m-d");
    $todate = date("Y-m-d");
    $_SESSION['searchMoney'] = " AND l.time >='$fromdate' AND l.time<='$todate'";

    $logmoney = mysql_query("SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName,l.note FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 " . $_SESSION['searchMoney'] . " ORDER BY " . $str . " " . $list[$str] . ',id DESC');

    //echo "SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 ".$_SESSION['searchMoney']." ORDER BY ".$str." ".$list[$str];
    if (isset($_REQUEST['page'])) {
        $page = $_REQUEST['page'];
    } else {
        $page = 1;
    }
    $rows_per_page = 10;
    $page_start = ( $page - 1 ) * $rows_per_page;
    $page_end = $page * $rows_per_page;
    $number_of_page = ceil(mysql_num_rows($logmoney) / $rows_per_page);
} else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'searchLastweek') {

    $list["l.time"] = "DESC";
    $list["l.type_money"] = "DESC";
    $list["l.type_trade"] = "DESC";
    $list["l.value"] = "DESC";
    $list["e.FullName"] = "DESC";

    $_SESSION['sort'] = $list;

    $fromdate = $_REQUEST['fromdate'];
    $todate = $_REQUEST['todate'];

    /* $d = new DateTime();
      $weekday = $d->format('w');
      $diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
      $d->modify("-$diff day");
      $fromdate=$d->format('Y-m-d');
      $d->modify('+6 day');
      $todate=$d->format('Y-m-d'); */

    //$fromdate = date('Y-m-d', strtotime('monday last week'));
    //$todate = date('Y-m-d', strtotime('sunday last week'));  
    function getWday($thoigian) {
        $kq = 7;
        if ($thoigian != "") {
            $ktdate = getdate(strtotime($thoigian));
            $kq = $ktdate[wday];
        }
        return $kq;
    }

    $time = time();
    $today = date("Y-m-d", time());

    //echo getWday($today);
    $time-= getWday($today) * 86400;
    $todate = date('Y-m-d', $time);


    $time-= 6 * 86400;
    $fromdate = date('Y-m-d', $time);


    $_SESSION['searchMoney'] = "  AND l.time >='$fromdate' AND l.time<='$todate'";

    $logmoney = mysql_query("SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName,l.note FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 " . $_SESSION['searchMoney'] . " ORDER BY " . $str . " " . $list[$str] . ',id DESC');
    if (isset($_REQUEST['page'])) {
        $page = $_REQUEST['page'];
    } else {
        $page = 1;
    }
    $rows_per_page = 10;
    $page_start = ( $page - 1 ) * $rows_per_page;
    $page_end = $page * $rows_per_page;
    $number_of_page = ceil(mysql_num_rows($logmoney) / $rows_per_page);
} else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'page') {

    $logmoney = mysql_query("SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName,l.note FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 " . $_SESSION['searchMoney'] . " ORDER BY " . $str . " " . $list[$str] . ',id DESC');
    if (isset($_REQUEST['page'])) {
        $page = $_REQUEST['page'];
    } else {
        $page = 1;
    }

    //echo "SELECT l.type_money,l.type_trade,l.`value`,l.time,e.FullName FROM log_manage_money l, employee e WHERE e.IDEmployee=l.id_employee AND l.status=0 ".$_SESSION['searchMoney']." ORDER BY ".$str." ".$list[$str]; 

    $rows_per_page = 10;
    $page_start = ( $page - 1 ) * $rows_per_page;
    $page_end = $page * $rows_per_page;
    $number_of_page = ceil(mysql_num_rows($logmoney) / $rows_per_page);
}
$_SESSION['sort'] = $list;
?>

<table cellpadding="0" cellspacing="1" border="0" class="ftable1" style="padding-left: 10px">
    <tr>
        <th style="width: 30px"><?= $l_no ?></th>
        <!-- <th style="width: 130px"><a onclick="sortMoney(<?= $page ?>,'l.type_trade','1','page')"><?php echo $l_nhap . '/' . $l_nhap; ?></a></th>-->
        <th style="width: 200px"><a onclick="sortMoney(<?= $page ?>,'l.time','1','page')"><?= $l_time ?></a></th>
        <th style="width: 130px"><a onclick="sortMoney(<?= $page ?>,'l.type_money','1','page')"><?= $l_tMoney ?></a></th>
        <th style="width: 200px"><a onclick="sortMoney(<?= $page ?>,'l.value','1','page')"><?= $l_money ?></a></th>
        <th style="width: 300px"><a onclick="sortMoney(<?= $page ?>,'e.FullName','1','page')"><?= $l_create_by ?></a></th>
        <th style="width: 300px"><?= $l_note ?></th>

    </tr>
<?php
$i = 0;
while ($r = mysql_fetch_array($logmoney)) {
    if ($i >= $page_start) {
        ?>
            <tr>
                <td style="width: 30px"><?= $i + 1 ?></td>
                <!-- <td style="width: 130px"><?php if ($r['type_trade'] == 0)
            echo $l_nhap; else
            echo $l_xuat; ?></td>-->
                <td style="width: 200px"><?php echo $r['time']; ?></td>
                <td style="width: 130px"><?php if ($r['type_money'] == 0)
            echo $l_tk1; else
            echo $l_tk2; ?></td>
                <td style="width: 200px"><?php echo number_format($r['value']) . " VNĐ "; ?></td>
                <td style="width: 300px"><?php echo $r['FullName']; ?></td>
                <td style="width: 300px"><?php echo $r['note']; ?></td>

            </tr>
        <?php
    }
    $i++;
    if ($i >= $page_end) {
        break;
    }
}
?>	

    <tr>
        <td colspan="6" class="bt_td_main">

            <span style="background:#fff; padding:1px; display: inline-block;">

    <?php
    if ($number_of_page > 1) {
        if ($page == 1)
            $p = 2;
        else if ($page == $number_of_page)
            $p = $number_of_page - 1;
        else
            $p=$page;
        if ($page != 1) {
            ?>
                        <a onclick="sortMoney(1,'<?= $str ?>',0,'page')" ><span class='button_next'><?php echo "<<<"; ?></span> </a>
                        <a onclick="sortMoney(<?= ($page - 1) ?>,'<?= $str ?>',0,'page')" ><span class='button_next'><?php echo "<<"; ?></span> </a>
                    <?php
                    } else {
                        echo "<span class='button_next'><<<</span>";
                        echo "<span class='button_next'><<</span>";
                    }
                    for ($i = $p - 1; $i <= $p + 1; $i++) {
                        if ($i <= $number_of_page && $i > 0)
                            if ($i == $page) {
                                echo "<span class='button_next'><b>" . $i . "</b></span>";
                            } else {
                                ?>
                                <a onclick="sortMoney(<?= $i ?>,'<?= $str ?>',0,'page')" ><span class="button_next" style="color: #508e21;">
                                <?php
                                echo $i;
                                ?>
                                    </span> </a>
                                <?php
                            }
                        ?>

                                <?php
                            }

                            if ($page != $number_of_page) {
                                ?>
                        <a onclick="sortMoney(<?= ($page + 1) ?>,'<?= $str ?>',0,'page')"><span class='button_next'><?php echo ">>"; ?></span> </a>
                        <a onclick="sortMoney(<?= ($number_of_page) ?>,'<?= $str ?>',0,'page')" ><span class='button_next'><?php echo ">>>"; ?></span> </a>
                    <?php
                    } else {
                        echo "<span class='button_next'>>></span> ";
                        echo "<span class='button_next'>>>></span> ";
                    }
                }
                ?>

            </span>
        </td>
    </tr>
</table>
                <?php mysql_close($connection); ?>	